package com.example.project.history;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import com.example.project.MultiPaneWidth;
import com.example.project.QueryListFragment;
import com.example.project.R;
import com.example.project.db.MyDBHelper;
import com.example.project.dto.ListnoCategoryDto;

public class History extends Activity {
	private QueryListFragment newFragment;
	private ArrayList<ListnoCategoryDto> arr_list;
	private MyDBHelper myDbManager;
	Cursor cursor = null;
	public static boolean ishistory = false;

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_history);
		ishistory = true;
		newFragment = (QueryListFragment) getFragmentManager()
				.findFragmentById(R.id.querylist);
		myDbManager = MyDBHelper.getInstance(this);
		arr_list = new ArrayList<ListnoCategoryDto>();
		
		
		cursor = myDbManager.select();
		if (cursor.moveToFirst()) {
			Log.d("print", "cursor했음");
			int n_list_no = cursor.getColumnIndex("list_no");
			int n_category = cursor.getColumnIndex("category");
			do {
				String list_no = cursor.getString(n_list_no);
				String category = cursor.getString(n_category);
				ListnoCategoryDto dto = new ListnoCategoryDto(list_no, category);
				Log.d("print", dto.toString());
				arr_list.add(dto);
			} while (cursor.moveToNext());
			Log.d("print", arr_list.size() + "를 구할수 있네");
			cursor.close();
		}		
		getActionBar().setHomeButtonEnabled(true);
		newFragment.setArr_list(arr_list);
	}
	
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.historyaction, menu);
		return super.onCreateOptionsMenu(menu);
	}
	
	@SuppressWarnings("unchecked")
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent intent;
		switch (item.getItemId()) {
		case R.id.historydel:
			intent = new Intent(this, HistoryDelActivity.class);
			intent.putParcelableArrayListExtra("arr_list", (ArrayList<? extends Parcelable>) arr_list);  
			startActivity(intent);
			return true;
		case android.R.id.home:
			intent = new Intent(this, MultiPaneWidth.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			return true;
		}
		return false;
	}

}
	
